import pandas as pd
import requests
from bs4 import BeautifulSoup
import re
import threading
import time
import xml.dom.minidom
import json
class DataBase():
	def __init__(self, file):
		self.file = file
		self.country_info = {}
		self.get_data()
	def get_data(self):
		#open xml file
		dom = xml.dom.minidom.parse(self.file)
		#get elements in the file
		root = dom.documentElement
		#search by Tag
		country = self.get_country()
		yy = dom.getElementsByTagName('Year')
		year = []
		for y in yy:
		    # elements of year
		    b = 0
		    while b < len(yy):
		        year.append(yy[b].firstChild.data)
		        b+=1
		vv = dom.getElementsByTagName('Value')
		value = []
		for v in vv:
		    # elements of value
		    d = 0
		    while d < len(vv):
		        value.append(vv[d].firstChild.data)
		        d+=1
		for i in range(len(country)):
		    self.country_info[country[i]] = {'year':year[i*28:(i+1)*28],'value':value[i*28:(i+1)*28]}
	def get_country(self):
		dom = xml.dom.minidom.parse(self.file)
		#get elements in the file
		root = dom.documentElement
		#search by Tag
		cc = dom.getElementsByTagName('Country')
		country = []
		for c in cc:
			# self.country = []
		    a = 0
		    while a < len(cc):
		        country.append(cc[a].firstChild.data)
		        a+=28
		return country
	def qurey_db(self, key):
		return json.dumps(self.country_info[key])